package com.stn.zjx.givingtreasure.glide;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichTextConfig;
import com.zzhoujay.richtext.callback.ImageGetter;
import com.zzhoujay.richtext.callback.ImageLoadNotify;

public class GlideImageGetter implements ImageGetter {
    public Activity activity;
    public GlideImageGetter(Activity activity){
         this.activity=activity;
    }
    @Override
    public void registerImageLoadNotify(ImageLoadNotify imageLoadNotify) {

    }

    @Override
    public Drawable getDrawable(ImageHolder holder, RichTextConfig config, TextView textView) {
        UrlDrawable drawable =new UrlDrawable();
       activity. runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide
                        .with(activity)
                        .asBitmap()
                        .load(holder.getSource())
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                drawable.bitmap = resource;
                                drawable.setBounds(0, 0, resource.getWidth(), resource.getHeight());
                                textView.invalidate();
                                textView.setText(textView.getText());
                            }
                        });
            }
        });

        return drawable;

    }

    @Override
    public void recycle() {

    }
}
